/**** COMMON ****/
var b_gaid="UA-12420642-2";
if(!$tt) {
	//attempt to load ga library
	try{
		if(!_gaq) {
			var $B_gaq = _gaq || [];
			$B_gaq.push(['_setAccount', b_gaid]);
			
			(function() {
				var $B_ga = document.createElement('script'); $B_ga.type = 'text/javascript'; $B_ga.async = true;
				$B_ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var $B_s = document.getElementsByTagName('script')[0]; $B_s.parentNode.insertBefore($B_ga, $B_s);
			})();
		} else {
			var $B_gaq = _gaq;
		}
	} catch(e) { /*ignore*/ }
	
	if(!$tt){
		if(turntable){ var $tt=turntable; }//grab turntable object
		if($tt){
			for(i in $tt){
				var $tt_roomkey=$tt[i]; break; //grab roomkey object
			}
		}
		if($tt_roomkey){
			for(i in $tt_roomkey){
				if($tt_roomkey[i]&&$tt_roomkey[i].callback) {
					var $tt_vote=$tt_roomkey[i]; break; //grab object for callback
				}
			}
		}
	}
}
if(!autoStatusDiv) {
	var autoStatusDiv=$('#bcj19-tt').html();
	if(!autoStatusDiv) {
		$('div.header').append('<div id="bcj19-tt"><table><tr id="bcj19-tt-tr"><td id="bcj19-tt-awesome" title="Auto-Awesome">&nbsp;</td><td id="bcj19-tt-dj" title="Auto-DJ">&nbsp;</td><td id="bcj19-tt-skip" title="Auto-Skip">&nbsp;</td></tr></table>');
	}
}
/**** END COMMON ****/

/*become dj if you've fallen off the stage*/
function becomeDj() {
	var ttFound;
	var ttPossibleDjCount = $tt_vote.dj_spots; //get total # of possible dj spots (generally 5)
	var ttCurrentDjCount = 0 //dynamic count of current djs

	if(ttAutoDjEnabled) { //override feature to stop becomeDj() from repeatedly executing
		for (x in $tt_vote.djs_uid) { //are you currently a dj?
			if ($tt_vote.myuserid == x) { //compare my uid to each current dj
				ttFound = 'found'; break; //jump out of loop if already a dj
			} else {
				ttFound = 'notFound'; ttCurrentDjCount = ttCurrentDjCount + 1;
			}
		}
		if (ttFound == "found") {
			/*do nothing - already a dj*/
		} else { //not a dj; see if there's room to become one
			if (ttCurrentDjCount < ttPossibleDjCount) {
				$tt_vote.callback('become_dj'); /*become a dj*/
			} else {
				/* do nothing - all spots taken */
			}
		}
		var t2=setTimeout("becomeDj()", 300); //call becomeDj() function every 0.3 secs
	} else { 
		//console.log('disable auto-dj'); 
		clearTimeout(t2);
	}
}
/*
function buildApprovedDjList(pv_approvedDjs, pv_djId) {
	var returnVal;
	if (pv_approvedDjs) {
		if (pv_approvedDjs.indexOf(pv_djId) > -1) {
			returnVal = pv_approvedDjs;
		} else {
			returnVal = pv_approvedDjs + "," + pv_djId;
		}
	} else {
		returnVal = pv_djId;
	}
	return (returnVal);
}
*/
/*become dj and initiate timed interval loop*/
if(ttAutoDjEnabled) { 
	ttAutoDjEnabled=false; 
	$('#bcj19-tt-dj').css('background-color','');
	$('#bcj19-tt-dj').attr('title','Auto-DJ Disabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid]);
		$B_gaq.push(['_trackEvent','Auto-DJ','Disabled']);

		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
} else { 
	var ttAutoDjEnabled=true;
	$('#bcj19-tt-dj').css('background-color','#2b60d3'); //royal blue
	$('#bcj19-tt-dj').attr('title','Auto-DJ Enabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid]);
		$B_gaq.push(['_trackEvent','Auto-DJ','Enabled']);
		
		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
}
if($tt_vote) { becomeDj(); }